package com.jesper.seckill.config;

import com.jesper.seckill.constant.MqConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

	/**
     * 连接工厂
     */
    @Autowired
    private ConnectionFactory connectionFactory;
    
    /**
     * 消息确认机制
     * Confirms给客户端一种轻量级的方式，能够跟踪哪些消息被broker处理，
     * 哪些可能因为broker宕掉或者网络失败的情况而重新发布。
     * 确认并且保证消息被送达，提供了两种方式：发布确认和事务。(两者不可同时使用)
     * 在channel为事务时，不可引入确认模式；同样channel为确认模式下，不可使用事务。
     * @return
     */
    @Bean
    public MsgSendConfirmCallBack msgSendConfirmCallBack(){
        return new MsgSendConfirmCallBack();
    }
    
    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        return template;
    }
    
    /**
     *@Description   消息转换器使用了RabbitMQ自带的Jackson2JsonMessageConverter转换器，但是没有指定消息的contentType类型
     *            所以需要手动设置content-type,这里用到了ContentTypeDelegatingMessageConverter
     **/
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPrefetchCount(20);
        ContentTypeDelegatingMessageConverter converter = new ContentTypeDelegatingMessageConverter();
        converter.addDelegate(MessageProperties.CONTENT_TYPE_JSON, new ContentTypeDelegatingMessageConverter());
        //factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setMessageConverter(converter);
        return factory;
    }

    /** 延时交换机 */
    @Bean
    public CustomExchange mallChatDelayedExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MqConstant.MALLCHAT_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }


    /** 用户商品秒杀成功队列 */
    @Bean
    public Queue secKillGoodsOrder() {
        return new Queue(MqConstant.SECKILL_GOODS_ORDER_QUEUE);
    }


    /** 队列绑定交换机及路由键-用户商品秒杀成功队列 */
    @Bean
    public Binding bindingSecKillGoodsOrder() {
        return BindingBuilder.bind(secKillGoodsOrder()).to(mallChatDelayedExchange()).with(MqConstant.SECKILL_GOODS_ORDER_ROUTINGKEY).noargs();
    }
}
